/* Copyright (c) 2009, Antonie Jovanoski
 *	
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact e-mail: Antonie Jovanoski <minimoog77@gmail.com>
 */

#include <QtDebug>
#include "qtwitfriendstimeline.h"

QTwitFriendsTimeline::QTwitFriendsTimeline(QObject *parent)
:	QTwitBase(parent)
{
}

QStringList QTwitFriendsTimeline::getFriendsTimeline() const
{
	return m_friendsTimeline;
}

void QTwitFriendsTimeline::update(const QDateTime &since, int sinceId, int count, int page)
{
	m_statuses.clear();

	QString urlString("https://twitter.com/statuses/friends_timeline.xml");
	bool moreParam = false;

	if(!since.isNull()){
		QLocale locale(QLocale::English);
		QString dateEnglish = locale.toString(since, "ddd,+dd+MMM+yyyy+hh:mm:ss+") + QString("GMT");
		urlString += "?";
		urlString += "since=";
		urlString += dateEnglish;
		moreParam = true;
	}

	if(sinceId != 0){

		if(moreParam){
			urlString += "&";
		} else {
			urlString += "?";
			moreParam = true;
		}
		urlString += QString("since_id=%1").arg(sinceId);
	}

	if(count != 0){

		if(moreParam){
			urlString += "&";
		} else {
			urlString += "?";
			moreParam = true;
		}

		urlString += QString("count=%1").arg(count);;
	}

	if(page != 0){

		if(moreParam){
			urlString += "&";
		} else {
			urlString += "?";
		}

		urlString += QString("page=%1").arg(page);
	}

	QUrl url(urlString);
	QNetworkRequest req(url);
	m_netManager.get(req);
}

void QTwitFriendsTimeline::reply(QNetworkReply *reply)
{
	if(reply->error() != QNetworkReply::NoError){
		qDebug() << "Reply Error";
		return;
	}

	QByteArray output = reply->readAll();

	QBuffer outputBuffer(&output);
	outputBuffer.open(QIODevice::ReadOnly);

	QXmlQuery query;
	query.bindVariable("inputDocument", &outputBuffer);
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/string(text)");

	if(!query.evaluateTo(&m_friendsTimeline))
		return;

	QStringList text;
	if(!query.evaluateTo(&text))
		return;

	QStringList screenName;
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/user/string(screen_name)");
	if(!query.evaluateTo(&screenName))
		return;

	QStringList id;
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/string(id)");
	if(!query.evaluateTo(&id))
		return;

	QStringList created;
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/string(created_at)");
	if(!query.evaluateTo(&created))
		return;

	QStringList imageUrl;
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/user/string(profile_image_url)");
	if(!query.evaluateTo(&imageUrl))
		return;

	QStringList source;
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/string(source)");
	if(!query.evaluateTo(&source))
		return;

	/*
	QStringList replyToStatusId;
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/string(in_reply_to_status_id)");
	if(!query.evaluateTo(&replyToStatusId))
		return;

	QStringList replyToUserId;
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/string(in_reply_to_user_id)");
	if(!query.evaluateTo(&replyToUserId))
		return;

	QStringList replyToScreenName;
	query.setQuery("declare variable $inputDocument external; doc($inputDocument)/statuses/status/string(in_reply_to_screen_name)");
	if(!query.evaluateTo(&replyToScreenName))
		return;
	*/

	for(int i = 0; i < text.size(); ++i){
		QTwitStatus s;
		s.setText(text.at(i));
		s.setScreenName(screenName.at(i));
		s.setId(id.at(i).toInt());
		s.setCreatedFromTwitter(created.at(i));
		s.setProfileImageUrl(imageUrl.at(i));
		s.setSource(source.at(i));

		/*
		if(!replyToStatusId.at(i).isEmpty())
			s.setReplyToStatusId(replyToStatusId.at(i).toInt());

		if(!replyToUserId.at(i).isEmpty())
			s.setReplyToUserId(replyToUserId.at(i).toInt());

		s.setReplyToScreenName(replyToScreenName.at(i));
		*/

		m_statuses.append(s);
	}

	reply->deleteLater();

	emit finished();
}

QList<QTwitStatus> QTwitFriendsTimeline::getStatuses() const
{
	return m_statuses;
}